#include "stm32f10x.h"                  // Device header
#include "encoder.h"
int16_t CountSensor_Count1;
int16_t CountSensor_Count2;
int16_t speed1,speed2;
void Encoder_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource3);

	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line3;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStructure);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn ;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
	
}
void Encoder2_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	

	GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource4);
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line4;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStructure);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn ;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
}
// void Timer_Init(void)
// {
// 	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
// 	TIM_InternalClockConfig(TIM2);
	
// 	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
// 	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
// 	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
// 	TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;
// 	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;
// 	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
// 	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
	
// 	TIM_ClearFlag(TIM2, TIM_FLAG_Update);
// 	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	
// 	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
// 	NVIC_InitTypeDef NVIC_InitStructure;
// 	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
// 	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// 	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
// 	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
// 	NVIC_Init(&NVIC_InitStructure);
	
// 	TIM_Cmd(TIM2, ENABLE);
// }
int16_t Encoder1GetValue (void)
{
	int16_t temp;
	temp = CountSensor_Count1;
	return temp;
}
int16_t Encoder2GetValue (void)
{
	int16_t temp;
	temp = CountSensor_Count2;
	return temp;
}
int16_t Encoder1GetSpeed (void)
{
	int16_t temp;
	temp = speed1;
	return temp;
}
int16_t Encoder2GetSpeed (void)
{
	int16_t temp;
	temp = speed2;
	return temp;
}
void EXTI3_IRQHandler(void)
{
	if (EXTI_GetITStatus(EXTI_Line3) == SET)
	{
		/*???????????????????????????��????????????????*/
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) == 0)
		{
			if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15) == 1)
				CountSensor_Count1 ++;
			else
				CountSensor_Count1 --;
		}
		EXTI_ClearITPendingBit(EXTI_Line3);
	}
}

void EXTI4_IRQHandler(void)
{
	if (EXTI_GetITStatus(EXTI_Line4) == SET)
	{
		/*???????????????????????????��????????????????*/
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0)
		{
			if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0)
				CountSensor_Count2 ++;
			else
				CountSensor_Count2 --;
		}
		EXTI_ClearITPendingBit(EXTI_Line4);
	}
}

void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		speed1 = Encoder1GetValue();
		CountSensor_Count1 = 0;
		speed2 = Encoder2GetValue();
		CountSensor_Count2 = 0;
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
}
